<?php
namespace ball;
use think\facade\Cache;
use ball\upay\Upay;

/**
 * 收钱吧api
 */
class UpayWap extends Upay
{
    protected $appid = '2025050900009027';
    protected $code = '21315954';
    protected $device_id = 'RHTYcustom';
    protected int $refreshTokenKeepTime = 86400*15;

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * 激活接口执行一次
     */
    public function activate( array $config=[]){

        $config = [
            'app_id'    => $this->appid,
            'code'      => $this->code,
            'device_id' => $this->device_id,
        ];
        $response = parent::activate($config);
        dump($response);die();
        if($response !== false && $response['result_code'] == '200'){
            $result = $response['biz_response'];
            return $result;
        }
    }
    /*签到接口
     * "terminal_sn" => "100090280046174505"
     * "terminal_key" => "4688b3f17f2d648cb644e702e63b4675"
     * "merchant_sn" => "18956397746"
     * "merchant_name" => "半夜鸡叫"
     * "store_sn" => "00010101001200200046406"
     * "store_name" => "半夜鸡叫"
     * */
    public function checkin(array $terminal = [])
    {
        $url = $this->apiDomain. '/terminal/checkin';
        $params = [
            'terminal_sn' => $terminal['terminal_sn'],       //终端号
            'device_id' => $terminal['device_id'],
        ];                //设备唯一身份ID
        ksort($params);
        $response = $this->httpBuild($url,$params,$terminal['terminal_sn'],$terminal['terminal_key']);
        dump($response);
        if($response !== false && $response['result_code'] == '200'){
            $result = $response['biz_response'];
            return $result;
        }
    }
    /*付款码
     */
    public function  pay($terminal_sn, $terminal_key)
    {
    
        $url = $this->apiDomain . "/upay/v2/pay";

        $params['terminal_sn'] = $terminal_sn;              //终端号
        $params['client_sn'] = ''; //商户系统订单号,必须在商户系统内唯一；且长度不超过64字节
        $params['total_amount'] = '';                      //交易总金额,以分为单位
        //$params['payway'] = '1';                          //支付方式,1:支付宝 3:微信 4:百付宝 5:京东钱包
        $params['dynamic_id'] = '';       //条码内容（支付包或微信条码号）
        $params['subject'] = '';                        //交易简介
        $params['operator'] = ''; 
        $params['notify_url']='';
    }
    /**
     * C扫B支付
     */
    function wap_api_pro($terminal_sn, $terminal_key)
    {
        $params['terminal_sn'] = $terminal_sn;      //收钱吧终端ID
        $params['client_sn'] = '';                  //商户系统订单号,必须在商户系统内唯一；且长度不超过64字节
        $params['total_amount'] = '';               //以分为单位,不超过10位纯数字字符串,超过1亿元的收款请使用银行转账
        $params['subject'] = '';                    //本次交易的概述
        $params['notify_url'] = '';                 //支付回调的地址
        $params['operator'] = '';                   //发起本次交易的操作员
        $params['return_url'] = "";                 //处理完请求后，当前页面自动跳转到商户网站里指定页面的http路径
        $params['reflect'] = "{ \"operator\": \"{$operate}\" }";
        ksort($params);                             //进行升序排序


        $param_str = "";
        foreach ($params as $k => $v) {
            $param_str .= $k .'='.$v.'&';
        }

        $sign = strtoupper(md5($param_str . 'key=' . $terminal_key));

        $paramsStr = $param_str . "sign=" . $sign;
        $res = "https://qr.shouqianba.com/gateway?" . $paramsStr;

        return $res;
    }
}
